package com.ext.aic.adapter.binder;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.ext.aic.R;
import com.ext.aic.beans.Attachment;
import com.ext.aic.beans.FlowItem;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.events.ViewTypeBinder;
import com.ext.aic.handler.FlowHandler;
import com.ext.aic.holder.ContentCenterViewHolder;
import com.ext.aic.ui.CopywritingDetailActivity;
import com.ext.aic.utils.FilteringUtils;
import com.ext.aic.utils.RedirectUtils;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

public class ContentItemBinder implements ViewTypeBinder<ContentCenterViewHolder, ProductItem> {

    private Context context;

    public ContentItemBinder(Context context) {
        this.context = context;
    }

    @Override
    public void bind(ContentCenterViewHolder holder, ProductItem productItem, int position) {
        holder.itemRl.setTag(productItem);
        holder.itemRl.setOnClickListener(this::onItemClick);

        bindImage(productItem, holder.itemRiv);
        holder.titleTv.setText(productItem.getTitle());
        holder.descTv.setText(FilteringUtils.getInstance().toText(productItem.getBody()));
        String recommendReason = productItem.getRecommendReason();

        boolean reasonEmptyFlag = false, labelEmptyFlag = false;
        if (TextUtils.isEmpty(recommendReason)) {
            reasonEmptyFlag = true;
            holder.reasonsTv.setVisibility(View.GONE);
        } else {
            holder.reasonsTv.setVisibility(View.VISIBLE);
            String reasons = String.format(context.getString(R.string.reasons_format), recommendReason);
            holder.reasonsTv.setText(reasons);
        }
        List<String> labels = productItem.getLabels();
        if (ObjectJudge.isNullOrEmpty(labels)) {
            labelEmptyFlag = true;
            holder.labelLl.setVisibility(View.GONE);
        } else {
            holder.labelLl.setVisibility(View.VISIBLE);
            List<FlowItem> tabItems = new ArrayList<>();
            for (String label : labels) {
                tabItems.add(new FlowItem(label));
            }
            FlowHandler flowHandler = new FlowHandler();
            flowHandler.bindTags(holder.labelFbl, tabItems);
        }

        if (reasonEmptyFlag && labelEmptyFlag) {
            holder.splitV.setVisibility(View.GONE);
        } else {
            holder.splitV.setVisibility(View.VISIBLE);
        }
    }

    private ProductItem getProductByTag(View view) {
        Object tag = view.getTag();
        if (!(tag instanceof ProductItem)) {
            return null;
        }
        return (ProductItem) tag;
    }

    public void onItemClick(View view) {
        ProductItem productItem = getProductByTag(view);
        Bundle bundle = new Bundle();
        bundle.putString("productId", productItem.getId());
        bundle.putString("creatorId", productItem.getCreatedBy());
        RedirectUtils.getInstance().startActivity(context, CopywritingDetailActivity.class, bundle);
    }

    private void bindImage(ProductItem productItem, RoundedImageView itemRiv) {
        List<Attachment> attachments = productItem.getAttachments();
        if (!ObjectJudge.isNullOrEmpty(attachments)) {
            Attachment attachment = attachments.get(0);
            Glide.with(context).clear(itemRiv);
            itemRiv.setTag(R.id.glide_image_tag, attachment.getFileUrl());
            Glide.with(context)
                    .load(attachment.getFileUrl())
                    .listener(new ContentGlideImageRequestListener(itemRiv, attachment.getFileUrl()))
                    .into(itemRiv);
        }
    }

    static class ContentGlideImageRequestListener implements RequestListener {

        private RoundedImageView itemRiv;
        private String imageUrl;

        public ContentGlideImageRequestListener(RoundedImageView itemRiv, String imageUrl) {
            this.itemRiv = itemRiv;
            this.imageUrl = imageUrl;
        }

        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
            String currentTag = (String) itemRiv.getTag(R.id.glide_image_tag);
            return !TextUtils.equals(imageUrl, currentTag);
        }
    }
}
